/************************************************************************************************************ 
												Master File
									Executes cleaning of data & analysis
									- 	Built on Stata 14 on macOS   -
										Benjamin Lerch (23.10.2023)
************************************************************************************************************/

clear all
set more off
set maxvar 32000

************************************************************************************************************
*												Global paths											   *
************************************************************************************************************

*General (substitute with own path)
global project "/Users/giuseppedigiacomo/Documents/Replication"


*Raw_data
global raw_data_automation "$project/raw_data/automation"
global raw_data_imports "$project/raw_data/imports"
global raw_data_lmarket "$project/raw_data/labor market"
global raw_data_education "$project/raw_data/education"
global raw_data_industry "$project/raw_data/industry"
global raw_data_geo "$project/raw_data/geo"
global contributed_data "$project/raw_data/contributed"

*Temp_data
global clean_data_automation "$project/final_data_provided/temp_data"
global clean_data_imports "$project/temp_data/imports"
global clean_data_lmarket "$project/final_data_provided/temp_data"
global clean_data_education "$project/final_data_provided/temp_data"

*Final_data_provided
global final_data_automation "$project/final_data_provided/automation"
global final_data_imports "$project/final_data_provided/imports"
global final_data_outcomes "$project/final_data_provided/outcomes"
global final_data_covariates "$project/final_data_provided/covariates"

*xwalks
global xwalk_geo "$project/xwalks/xwalks_geography"
global xwalk_ind "$project/xwalks/xwalks_industry"
global xwalk_occ "$project/xwalks/xwalks_occupation"
global xwalk_wage "$project/xwalks/xwalks_wages"

*Lists
global list "$project/raw_data/list"


* Creates folders
shell mkdir "$project/results" // results
shell mkdir "$project/results/appendix" // results
shell mkdir "$project/results/appendix/figures" // results
shell mkdir "$project/results/appendix/tables" // results
shell mkdir "$project/results/main" // results
shell mkdir "$project/results/main/figures" // results
shell mkdir "$project/results/main/tables" // results

*Tables and figures
global tables_main "$project/results/main/tables"
global figures_main "$project/results/main/figures"
global tables_appendix "$project/results/appendix/tables"
global figures_appendix "$project/results/appendix/figures"

*Dofiles
global do_clean_automation "$project/dofiles/cleaners/automation"
global do_clean_covariates "$project/dofiles/cleaners/covariates"
global do_clean_lmarket "$project/dofiles/cleaners/labor market"
global do_clean_education "$project/dofiles/cleaners/education"
global do_clean_imports "$project/dofiles/cleaners/imports"

global do_build_automation "$project/dofiles/builders/automation"
global do_build_covariates "$project/dofiles/builders/covariates"
global do_build_lmarket "$project/dofiles/builders/labor market"
global do_build_education "$project/dofiles/builders/education"

global do_build_main "$project/dofiles/analysis/main"
global do_build_appendix "$project/dofiles/analysis/appendix"

************************************************************************************************************
*											Regression covariates										   *
************************************************************************************************************

*1990
global demographics_1990     ipums_female_1990 ipums_black_1990 ipums_hispanic_1990 ipums_low_1990 ipums_logpop_1990 ipums_a25_34_1990 ipums_a35_44_1990 ipums_a45_54_1990
global industry_shares_1990  ind_share_manufacturing_1990 ind_share_services_1990 ind_share_research_1990 ind_share_utilities_1990 ind_share_mining_1990 ind_share_construction_1990
global occupations_1990      expo_share_occ_routine_1990 expo_share_occ_offshore_1990
global institutions_1990   public private uniTop30Dummy largest150 anyAid


*1970
global demographics_1970     ipums_female_1970 ipums_black_1970 ipums_low_1970 ipums_logpop_1970 ipums_a25_34_1970 ipums_a35_44_1970 ipums_a45_54_1970
global industry_shares_1970  ind_share_manufacturing_1970 ind_share_low_usage_1970 ind_share_services_1970
global occupations_1970      expo_share_occ_routine_1970 expo_share_occ_offshore_1970

*Period
global demographics          ipums_female ipums_black ipums_hispanic ipums_low ipums_logpop ipums_a25_34 ipums_a35_44 ipums_a45_54
global industry_shares       ind_share_manufacturing ind_share_services ind_share_research ind_share_utilities ind_share_mining ind_share_construction
global occupations           expo_share_occ_routine expo_share_occ_offshore

*CZ of origin and destination
global controls_combi_1990 		ipums_female_1990_d ipums_black_1990_d ipums_hispanic_1990_d ipums_low_1990_d ipums_logpop_1990_d ipums_a25_34_1990_d ipums_a35_44_1990_d ipums_a45_54_1990_d ///
								ind_share_manufacturing_1990_d ind_share_services_1990_d ind_share_research_1990_d ind_share_utilities_1990_d ind_share_mining_1990_d ind_share_construction_1990_d ///
								expo_share_occ_routine_1990_d expo_share_occ_offshore_1990_d public_d private_d uniTop30Dummy_d largest150_d anyAid_d ///
								ipums_female_1990_o ipums_black_1990_o ipums_hispanic_1990_o ipums_low_1990_o ipums_logpop_1990_o ipums_a25_34_1990_o ipums_a35_44_1990_o ipums_a45_54_1990_o ///
								ind_share_manufacturing_1990_o ind_share_services_1990_o ind_share_research_1990_o ind_share_utilities_1990_o ind_share_mining_1990_o ind_share_construction_1990_o ///
								expo_share_occ_routine_1990_o expo_share_occ_offshore_1990_o public_o private_o uniTop30Dummy_o largest150_o anyAid_o 


************************************************************************************************************
*													Analysis											   *
************************************************************************************************************

******************************************
*	   		 Main figures 				 *
******************************************

*Figure 1: Robot adoption at the CZ level, 1993-2007
run "$do_build_main/Figure 1 - map robot exposure.do"

*Figure 2: Robots and schooling by age
run "$do_build_main/Figure 2a - enrollment age.do"

*Figure 2: Robots and schooling by demographics
run "$do_build_main/Figure 2b - enrollment demographics.do"


*Figure 3: Robots and field of study
run "$do_build_main/Figure 3 - field of study.do"

*Figure 5: College education choice
*This figure has been generated in Microsoft Powerpoint

******************************************
*	   		 Main tables 				 *
******************************************

*Table 1: Descriptive statistics
run "$do_build_main/Table 1 - main descriptives.do"

*Table 2: Robots and college enrollment
run "$do_build_main/Table 2 - college enrollment.do"

*Table 3: Robots and college enrollment: Institution characteristics
run "$do_build_main/Table 3 - institution characteristics.do"

*Table 4: Robots and college enrollment by employment status
run "$do_build_main/Table 4 - students employment.do"

*Table 5: Robots and student migration
run "$do_build_main/Table 5 - migration.do"

*Table 6: Robots and graduation rates
run "$do_build_main/Table 6 - graduation.do"

*Table 7: Robots and income
run "$do_build_main/Table 7 - income.do"

******************************************
*	   		Appendix figures 			 *
******************************************

*Figure A1: Robots along the skill distribution
run "$do_build_appendix/Figure A1 - skill distribution.do"

*Figure A2: Share of migrating population, 1990
run "$do_build_appendix/Figure A2 - map migrants.do"

*Figure A3: pptx

*Figure A4: Robot exposure by industry exclusion
run "$do_build_appendix/Figure A4 - industry exclusion.do"

*Figure A5: Robot exposure by industry exclusion
run "$do_build_appendix/Figure A5 - state exp demographics.do"


******************************************
*	   		Appendix tables 			 *
******************************************

*Table A1: Aggregation of CIP codes.
*This table has been generated manually in LaTeX

*Table A2: Descriptive statistics: Industrial robots
run "$do_build_appendix/Table A2 - robots descriptives.do"

*Table A3: Robots and college enrollment until 2014
run "$do_build_appendix/Table A3 - extended period.do"

*Table A4: Robots, imports and college enrollment
run "$do_build_appendix/Table A4 - robots and imports.do"

*Table A5: Robots and incoming students: In-state and out-state students
run "$do_build_appendix/Table A5 - migration states.do"

*Table A6: Robots and employment by education level
run "$do_build_appendix/Table A6 - employment.do"

*Table A7: Robots and college enrollment: Product market competition from Europe
run "$do_build_appendix/Table A7 - product market competition.do"

*Table A8: Robots and college enrollment pre-trends
run "$do_build_appendix/Table A8 - pre-trends.do"

*Table A9: Robots and college enrollment: Placebo test
run "$do_build_appendix/Table A9 - placebo and initial shares.do"

*Table A10: Robots and students’ CZ-specific migration flows
run "$do_build_appendix/Table A10 - CZ-to-CZ flows.do"

*Table A11: Robots and student migration using a 5-year normalization
run "$do_build_appendix/Table A11 - migration 5 years.do"

*Table A12: Robots and college enrollment: Alternative construction of robot exposure measures
run "$do_build_appendix/Table A12 - alternative instruments.do"

*Table A13: Robots and college enrollment: Exclusion of CZs with highest robot exposure
run "$do_build_appendix/Table A13 - exclusion.do"

*Table A14: Robots and college enrollment: Unweighted results
run "$do_build_appendix/Table A14 - unweighted.do"

*Table A15: Robots and college enrollment at state level 
run "$do_build_appendix/Table A15 - state level.do"


